package com.zuxia.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zuxia.dao.IApplyDao;
import com.zuxia.entity.Apply;
@Component
public class ApplyDao implements IApplyDao{
	@Autowired
	private SessionFactory sf;

	@Override
	public List<Apply> selectApply(int pageNo, String zname) {
		// TODO Auto-generated method stub
		Session session = sf.getCurrentSession();
		Criteria criteria = null;
		if(zname != null && zname != ""){
			criteria = session.createCriteria(Apply.class).add(Restrictions.like("applyName", "%"+zname+"%"));
		}else{
			criteria = session.createCriteria(Apply.class);
		}
		criteria.setFirstResult((pageNo - 1) * 5);
		criteria.setMaxResults(5);
		return  criteria.list();
	}

	@Override
	public int apysize() {
		// TODO Auto-generated method stub
		Session session = sf.getCurrentSession();
		return session.createCriteria(Apply.class).list().size();
	}

	@Override
	public void delapy(Apply apply) {
		// TODO Auto-generated method stub
		Session session = sf.openSession();
		Transaction tran = session.beginTransaction();
		session.delete(apply);
		tran.commit();
		session.close();
	}

	@Override
	public Apply updapy(Apply apply) {
		// TODO Auto-generated method stub
		Session session = sf.getCurrentSession();
		return session.get(Apply.class, apply.getApplyId());
	}

	@Override
	public void addapy(Apply apply) {
		// TODO Auto-generated method stub
		Session session = sf.openSession();
		Transaction tran = session.beginTransaction();
		session.save(apply);
		tran.commit();
		session.close();
	}

}
